
*	************************************************************************
* 	File-Name: 			Chap2_replication.do
*	Latest version:  	21 December 2018
*
*	Authors: 			Michael Aklin, Patrick Bayer, S.P. Harish, and Johannes Urpelainen
*	Book title: 		Escaping the Energy Poverty Trap: When and How Governments Power the Lives of the Poor
* 	Chapter:			Chapter 2
*	Purpose:			Generates maps and boxplots

*	Data:				weo2017_gtf2017.xlsx
*	Other notes:		Plots to produce maps require 'spmap.pkg'
*	************************************************************************


*	************************************************************************
*	A. Load data, create variables, create labels
*	************************************************************************

clear all
set more off, permanently

*	************************************************************************
*	DISCLAIMER
*	************************************************************************

/*	The data used here come from the WEO Energy Access Outlook 2017 and the Global Tracking Framework
	WEO data:
	- Electrification rates are from 2016
	- Cooking data are from 2015
	- Cooking data measures share of population w/o (!) access to modern cooking fuels

	GTF data:
	- Electrification rates are from 2014
	- Cooking data are from 2014
	- Cooking data measures share of population with (!) access to modern cooking fuels

*/


* Load data
import excel "./weo2017_gtf2017.xlsx", sheet("Sheet1") firstrow

* Label variables
label variable weo_elec "Electrification rate (\%)"
label variable weo_elec_u "Urbal electrification rate (\%)"
label variable weo_elec_r "Rural electrification rate (\%)"
label variable weo_cooking "Population w/o clean cooking access (\%)"

gen weo_cooking_clean=100-weo_cooking
label variable weo_cooking_clean "Population with clean cooking access (\%)"

label variable gtf_elec "Electrification rate (\%)"
label variable gtf_elec_u "Urbal electrification rate (\%)"
label variable gtf_elec_r "Rural electrification rate (\%)"
label variable gtf_cooking "Population with clean cooking access (\%)"


* Variable to check distributions from two data sources
* Electrification data
gen weo_bin=0
replace weo_bin=1 if weo_elec <10
replace weo_bin=2 if weo_elec >=10 & weo_elec <25
replace weo_bin=3 if weo_elec >=25 & weo_elec <50
replace weo_bin=4 if weo_elec >=50 & weo_elec <75
replace weo_bin=5 if weo_elec >=75 & weo_elec<90
replace weo_bin=6 if weo_elec >=90


gen gtf_bin=0
replace gtf_bin=1 if gtf_elec <10
replace gtf_bin=2 if gtf_elec >=10 & gtf_elec <25
replace gtf_bin=3 if gtf_elec >=25 & gtf_elec <50
replace gtf_bin=4 if gtf_elec >=50 & gtf_elec <75
replace gtf_bin=5 if gtf_elec >=75 & gtf_elec<90
replace gtf_bin=6 if gtf_elec >=90

tabulate weo_bin gtf_bin, chi
pwcorr weo_bin gtf_bin, sig

gen diff=weo_bin-gtf_bin
list Countryname weo_elec gtf_elec if diff !=0


* Variable to check distributions from two data sources
* Cooking data
gen weo_bin2=0
replace weo_bin2=1 if weo_cooking_clean <10
replace weo_bin2=2 if weo_cooking_clean >=10 & weo_cooking_clean <25
replace weo_bin2=3 if weo_cooking_clean >=25 & weo_cooking_clean <50
replace weo_bin2=4 if weo_cooking_clean >=50 & weo_cooking_clean <75
replace weo_bin2=5 if weo_cooking_clean >=75 & weo_cooking_clean <90
replace weo_bin2=6 if weo_cooking_clean >=90


gen gtf_bin2=0
replace gtf_bin2=1 if gtf_cooking <10
replace gtf_bin2=2 if gtf_cooking >=10 & gtf_cooking <25
replace gtf_bin2=3 if gtf_cooking >=25 & gtf_cooking <50
replace gtf_bin2=4 if gtf_cooking >=50 & gtf_cooking <75
replace gtf_bin2=5 if gtf_cooking >=75 & gtf_cooking <90
replace gtf_bin2=6 if gtf_cooking >=90

tabulate weo_bin2 gtf_bin2, chi
pwcorr weo_bin2 gtf_bin2, sig

gen diff2=weo_bin2-gtf_bin2
list Countryname weo_cooking_clean gtf_cooking if diff2 !=0


rename ISO countrycode
saveold "./elec_map", replace

*	************************************************************************
*	B. Prepare map variables
*	************************************************************************

* load shapefile database
clear all
use "./world_db.dta"
rename ISO3 countrycode

* Drop Antartica from plot area
drop if id==145
drop if FIPS==""
drop if NAME=="Greenland"
drop if NAME=="Iceland"

merge 1:1 countrycode using "./elec_map.dta"

* Create OECD and former Soviet Union state dummies
gen oecd=0
replace oecd=1 if NAME=="Australia"
replace oecd=1 if NAME=="Austria"
replace oecd=1 if NAME=="Belgium"
replace oecd=1 if NAME=="Canada"
replace oecd=1 if NAME=="Czech Republic"
replace oecd=1 if NAME=="Denmark"
replace oecd=1 if NAME=="Estonia"
replace oecd=1 if NAME=="Finland"
replace oecd=1 if NAME=="France"
replace oecd=1 if NAME=="Germany"
replace oecd=1 if NAME=="Greece"
replace oecd=1 if NAME=="Hungary"
replace oecd=1 if NAME=="Iceland"
replace oecd=1 if NAME=="Ireland"
replace oecd=1 if NAME=="Israel"
replace oecd=1 if NAME=="Italy"
replace oecd=1 if NAME=="Japan"
replace oecd=1 if NAME=="Korea, Republic of"
replace oecd=1 if NAME=="Luxembourg"
replace oecd=1 if NAME=="Netherlands"
replace oecd=1 if NAME=="New Zealand"
replace oecd=1 if NAME=="Norway"
replace oecd=1 if NAME=="Poland"
replace oecd=1 if NAME=="Portugal"
replace oecd=1 if NAME=="Slovakia"
replace oecd=1 if NAME=="Slovenia"
replace oecd=1 if NAME=="Spain"
replace oecd=1 if NAME=="Sweden"
replace oecd=1 if NAME=="Switzerland"
replace oecd=1 if NAME=="United Kingdom"
replace oecd=1 if NAME=="United States"


gen soviet=0
replace soviet=1 if NAME=="Estonia"
replace soviet=1 if NAME=="Latvia"
replace soviet=1 if NAME=="Lithuania"
replace soviet=1 if NAME=="Belarus"
replace soviet=1 if NAME=="Ukraine"
replace soviet=1 if NAME=="Armenia"
replace soviet=1 if NAME=="Azerbaijan"
replace soviet=1 if NAME=="Georgia"
replace soviet=1 if NAME=="Kazakhstan"
replace soviet=1 if NAME=="Kyrgyzstan"
replace soviet=1 if NAME=="Republic of Moldova"
replace soviet=1 if NAME=="Tajikistan"
replace soviet=1 if NAME=="Turkmenistan"
replace soviet=1 if NAME=="Uzbekistan"
replace soviet=1 if NAME=="Russia"

* Fill in data for Mexiko, Turkey, and Chile (NB: GTF data for 2014)
replace weo_elec=gtf_elec if NAME=="Turkey"
replace weo_elec=gtf_elec if NAME=="Chile"
replace weo_elec=gtf_elec if NAME=="Mexico"

* Electrification rate (work-around to fill with color in map)
replace weo_elec=0.01 if oecd==1
replace weo_elec=0.02 if soviet==1

* Adjust some of the missing cooking data
replace weo_cooking_clean=gtf_cooking if NAME=="Turkey"
replace weo_cooking_clean=gtf_cooking if NAME=="Chile"
replace weo_cooking_clean=gtf_cooking if NAME=="Mexico"
replace weo_cooking_clean=gtf_cooking if NAME=="United Arab Emirates"
replace weo_cooking_clean=gtf_cooking if NAME=="Saudi Arabia"
replace weo_cooking_clean=99 if NAME=="Libyan Arab Jamahiriya" /* WEO 2010 data as missing for 2015 */

* Cooking fuels (work-around to fill with color in map)
replace weo_cooking_clean=0.01 if oecd==1
replace weo_cooking_clean=0.02 if soviet==1

*	************************************************************************
*	1. Map for electrification data
*	************************************************************************

* Color scheme from here
*http://i0.wp.com/statadaily.ikonomiya.com/wp-content/uploads/2010/09/figure2_fullpalette.jpg

* FIGURE 2.1
spmap weo_elec using "./world_coord.dta" if AREA>2500, id(id) ///
title("Electricity Access around the World") ///
legenda(on) ndfcolor(white) ndo(black) ///
fcolor(eltgreen*0.5 erose*0.5 midblue*0.25 midblue*0.5 midblue*0.75 midblue*1.0 midblue*1.25 midblue*1.75) ///
clmethod(custom) clbreaks(0 0.01 0.02 10 25 50 75 90 100) ///
legend(label(1 "No data") label(2 "OECD") label(3 "Former Soviet") label(4 "<10%") label(5 "10%-25%") label(6 "25%-50%") ///
label(7 "50%-75%") label(8 "75%-90%") ///
label(9 "90%<")) legend(position(8))

*graph export "../../../TablesFigures/Ch2/map_elec_NEW.eps", replace

*	************************************************************************
*	2. Boxplot for electrification data
*	************************************************************************

* FIGURE 2.2
gen eurasia=0 if Region!=""
replace eurasia=1 if Region=="Europe & Central Asia (all income levels)"

graph box weo_elec if eurasia==0, over(Region, total relabel(1 `""East" "Asia""' 2 `""Latin" "America""' 3 `""Middle" "East""' 4 `""South" "Asia""' 5 `""Sub-Saharan" "Africa""' 6 `""Full" "Sample""')) nofill ///
title("Electricity Access by Region", color(black)) ytitle("Electrification rates (percent)") ///
graphregion(color(white)) bgcolor(white)

*graph export "../../../TablesFigures/Ch2/boxplot_elec_NEW.eps", replace



*	************************************************************************
*	3. Map for cooking 
*	************************************************************************


* FIGURE 2.3
spmap weo_cooking_clean using "./world_coord.dta" if AREA>2500, id(id) ///
title("Access to Clean Cooking Fuels around the World") ///
fcolor(eltgreen*0.5 erose*0.5 midblue*0.25 midblue*0.5 midblue*0.75 midblue*1.0 midblue*1.25 midblue*1.75) ///
clmethod(custom) clbreaks(0 0.01 0.02 10 25 50 75 90 100) ///
legend(label(1 "No data") label(2 "OECD") label(3 "Former Soviet") label(4 "<10%") label(5 "10%-25%") label(6 "25%-50%") ///
label(7 "50-75%") label(8 "75%-90%") ///
label(9 "<90%")) legend(position(8))

*graph export "../../../TablesFigures/Ch2/map_cooking_NEW.eps", replace

*	************************************************************************
*	4. Boxplot for cooking 
*	************************************************************************

* FIGURE 2.4
graph box weo_cooking_clean if eurasia==0, over(Region, total relabel(1 `""East" "Asia""' 2 `""Latin" "America""' 3 `""Middle" "East""' 4 `""South" "Asia""' 5 `""Sub-Saharan" "Africa""' 6 `""Full" "Sample""')) nofill /// ///
title("Access to Clean Cooking Fuels by Region", color(black)) ytitle("Population with access to clean cooking fuels (percent)") ///
graphregion(color(white)) bgcolor(white)

*graph export "../../../TablesFigures/Ch2/boxplot_cooking_NEW.eps", replace

*	************************************************************************
*	5. Claims in text
*	************************************************************************

sort Region
by Region: sum weo_elec

list weo_elec if Countryname=="China"
list weo_elec if Countryname=="Malaysia"
list weo_elec if Countryname=="Korea, Dem. Rep."
list weo_elec if Countryname=="Haiti" 
list weo_elec if Countryname=="Yemen, Rep."
list weo_elec if Countryname=="Djibouti"
sum weo_elec if Region=="East Asia & Pacific (all income levels)" | Region=="South Asia", d
browse weo_elec Countryname if Region=="East Asia & Pacific (all income levels)" | Region=="South Asia"

browse weo_elec Countryname if Region=="Sub-Saharan Africa (all income levels)"
sum weo_elec Countryname if Region=="Sub-Saharan Africa (all income levels)", d
list weo_elec weo_elec_u weo_elec_r if Countryname=="Kenya"
list weo_elec weo_elec_u weo_elec_r if Countryname=="Tanzania"

sum weo_elec_u if Region=="Sub-Saharan Africa (all income levels)", d
sum weo_elec_r if Region=="Sub-Saharan Africa (all income levels)", d

sum weo_elec_r if Region=="Middle East & North Africa (all income levels)", d
sum weo_elec_r if Region=="Latin America & Caribbean (all income levels)", d

list weo_cooking_clean if Countryname=="Brazil"
list weo_cooking_clean if Countryname=="Saudi Arabia"

browse weo_cooking_clean Countryname if Region=="Sub-Saharan Africa (all income levels)"

count if Region == "Sub-Saharan Africa (all income levels)"
count if weo_cooking_clean < 10 & weo_cooking_clean != . & Region == "Sub-Saharan Africa (all income levels)"

sum weo_cooking_clean if Region=="Sub-Saharan Africa (all income levels)", d
sum weo_cooking_clean if Region=="East Asia & Pacific (all income levels)" | Region=="South Asia", d

list weo_cooking_clean if Countryname=="Myanmar"
list weo_cooking_clean if Countryname=="Haiti"
list weo_cooking_clean if Countryname=="Lao PDR"


browse weo_cooking_clean Countryname if Region=="Latin America & Caribbean (all income levels)"
browse weo_cooking_clean Countryname if Region=="East Asia & Pacific (all income levels)" | Region=="South Asia"

